//
//  ManagerMyNotesViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-11-21.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "ManagerMyNotesViewController.h"
#import "MyNotesViewController.h"

@interface ManagerMyNotesViewController ()<UITextViewDelegate>
{
    int textFont;
}
@property (nonatomic, strong)UILabel * shengJingMessageLabel;
@property (nonatomic, strong)UITextView * myNotesView;


@end

@implementation ManagerMyNotesViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏字体颜色
    self.navigationItem.title = @"我的笔记";
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = FONT_16;
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    [self setupUI];
}
- (void)setupUI
{
    if (ScreenWidth == 320) {
        //5
        textFont = 13.5;
    }else if(ScreenWidth == 375){
        // 6 7
        textFont = 14;
    }else {
        //plus
        textFont = 14.5;
    }
    TPKeyboardAvoidingScrollView * backScrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenHeight)];
    backScrollView.contentSize = CGSizeMake(ScreenWidth,ScreenHeight);
    backScrollView.userInteractionEnabled = YES;
    [self.view addSubview:backScrollView];

    NSString * tempStr = [NSString stringWithFormat:@"%@ \n \n %@", self.shengJingTitle, self.notesContentStr];
    CGRect labTitleRect = [tempStr boundingRectWithSize:CGSizeMake(self.view.width-20, 4000) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:textFont] forKey:NSFontAttributeName] context:nil];
    self.tempView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.width, labTitleRect.size.height+20)];
    self.tempView.backgroundColor = JK_FENRED_COLOR;
    [backScrollView addSubview:self.tempView];

    self.shengJingMessageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.width-2*10, labTitleRect.size.height+20)];
    if (ScreenWidth == 320) {
        //5
        _shengJingMessageLabel.font = FONT_(13.5);
    }else if(ScreenWidth == 375){
        // 6 7
        _shengJingMessageLabel.font = FONT_(14);
    }else {
        //plus
        _shengJingMessageLabel.font = FONT_(14.5);
    }

    _shengJingMessageLabel.numberOfLines = 0;
    _shengJingMessageLabel.text = tempStr;
    _shengJingMessageLabel.textColor = redUIColor;
    [self.tempView addSubview:_shengJingMessageLabel];

    self.myNotesView = [[UITextView alloc] init];
    _myNotesView.text = self.shengJingMessageStr;
    _myNotesView.textColor = titleColorLSZ;
    _myNotesView.delegate = self;
    _myNotesView.textContainerInset = UIEdgeInsetsMake(10,0, 0, 0);
    [backScrollView addSubview:_myNotesView];
    float   height = [ self heightForTextView:self.myNotesView WithText:self.shengJingMessageStr];
    if (ScreenWidth == 320) {
        //5
        _myNotesView.font = FONT_(13.5);
        height = height;
    }else if(ScreenWidth == 375){
        // 6 7
        _myNotesView.font = FONT_(14);
        height = height+15;
    }else {
        //plus
        _myNotesView.font = FONT_(14.5);
        height = height+30;
    }

    if (height >ScreenHeight -10-self.tempView.bottom) {
        self.myNotesView.frame = CGRectMake(_shengJingMessageLabel.left, self.tempView.bottom, _shengJingMessageLabel.width, ScreenHeight -10-self.tempView.bottom-NAVIGATION_BAR_HEIGHT);
    }
    else if(height<200){
        self.myNotesView.frame = CGRectMake(_shengJingMessageLabel.left, self.tempView.bottom, _shengJingMessageLabel.width, 200);
    }
    else{
        self.myNotesView.frame = CGRectMake(_shengJingMessageLabel.left, self.tempView.bottom, _shengJingMessageLabel.width,height);
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CGRect frame = textView.frame;
    float  height = [ self heightForTextView:textView WithText:textView.text];
    if (height>ScreenHeight -10-self.tempView.bottom) {
        return YES;
    }
    else if(height<200){
        self.myNotesView.frame = CGRectMake(_shengJingMessageLabel.left, self.tempView.bottom, _shengJingMessageLabel.width, 200);
        return YES;
    }
    frame.size.height = height;
    [UIView animateWithDuration:0.5 animations:^{
        textView.frame = frame;
    } completion:nil];
    return YES;
}
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(ScreenWidth -20, CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:textFont]}
                                        context:nil];
    float textHeight = size.size.height + 30.0;
    return textHeight;
}
#pragma mark - 下面的注销了
- (void)rightBtnAction
{
    __weak typeof(self) weakSelf = self;
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
    if ([weakSelf.partitionStr isEqualToString:@"1"]) {
        [LSZRequestData initmodificationNotes:@"notesUpdate" parentId:weakSelf.myNotesId content:weakSelf.myNotesView.text :^(BOOL isOK, NSString * Str, NSDictionary * dic) {
            if (isOK == YES) {
                hud.label.text= [NSString stringWithFormat:@"%@",Str];
                [weakSelf performSelector:@selector(leftBtnAction) withObject:nil afterDelay:1.0];
            }else{
                [weakSelf addHubPromptMessage:Str];
            }
        }];
    }
    else{
        [LSZRequestData initAddNotes:weakSelf.shengJingTitle relation:weakSelf.notesContentStr content:weakSelf.myNotesView.text:^(BOOL isOK, NSString * Str, NSDictionary * dic) {
             if (isOK == YES) {
                hud.label.text= [NSString stringWithFormat:@"%@",Str];
                [weakSelf performSelector:@selector(leftBtnAction) withObject:nil afterDelay:2.0];
            }else{
                [weakSelf addHubPromptMessage:Str];
            }
        }];
    }
}
- (void)leftBtnAction
{
    [SVProgressHUD dismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

/*
- (void)rightBtnAction
{
    if (self.myNotesId.length > 0) {        //修改笔记
        NSDictionary * dic = @{@"type":@"notesUpdate", @"uid":[UserPageManager shareUserPageManager].userID, @"parentId":self.myNotesId, @"content":self.myNotesView.text};
 
 NSDictionary * dic = @{@"type":@"notesAdd", @"uid":[UserPageManager shareUserPageManager].userID, @"title":self.shengJingTitle, @"relation":self.shengJingMessageStr , @"content":self.myNotesView.text};

        [HttpTool postWithURL:kBasePostFuyin params:dic success:^(id json) {
            NSString * status = nil;
            if ([[json objectForKey:@"status"] isKindOfClass:[NSNumber class]]) {
                status = [[json objectForKey:@"status"] stringValue];
            }
            if ([[json objectForKey:@"status"] isKindOfClass:[NSString class]]) {
                status = [json objectForKey:@"status"];
            }
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
            
            if ([status isEqualToString:@"200"]) {
                hud.labelText=@"笔记修改成功";
                [self.myNotesVC reloadMyNotesMess];
            }
            if ([status isEqualToString:@"400"]) {
                hud.labelText=@"笔记修改失败";
            }
            [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
            [self performSelector:@selector(leftBtnAction) withObject:nil afterDelay:1.0];

        } failure:^(NSError *error) {
            
        }];
        NSLog(@"修改笔记");

    }else{      //添加笔记
        
        NSDictionary * dic = @{@"type":@"notesAdd", @"uid":[UserPageManager shareUserPageManager].userID, @"title":self.shengJingTitle, @"relation":self.shengJingMessageStr , @"content":self.myNotesView.text};
 
        [HttpTool postWithURL:kBasePostFuyin params:dic success:^(id json) {
            NSString * status = nil;
            if ([[json objectForKey:@"status"] isKindOfClass:[NSNumber class]]) {
                status = [[json objectForKey:@"status"] stringValue];
            }
            if ([[json objectForKey:@"status"] isKindOfClass:[NSString class]]) {
                status = [json objectForKey:@"status"];
            }
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
            
            if ([status isEqualToString:@"200"]) {
                hud.labelText=@"成功添加笔记";
            }
            if ([status isEqualToString:@"400"]) {
                hud.labelText=@"笔记添加失败";
            }
            
            [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
            [self performSelector:@selector(leftBtnAction) withObject:nil afterDelay:1.0];

        } failure:^(NSError *error) {
            
        }];
        
        NSLog(@"添加新笔记");
    }
    
    [self.view endEditing:YES];
}
*/

@end
